/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function (nums, target) {
  let l = 0,
    r = nums.length - 1 //取左右索引
  while (l <= r) {
    let mid = Math.floor((l + r) / 2) //取中位数mid
    if (nums[mid] == target) return mid //找到
    if (nums[mid] < target) l = mid + 1 //找右边
    if (nums[mid] > target) r = mid - 1 //找左边
  }
  return l //找不到
};